const app = getApp()
Page({
  data: {
    typeIndex: 0,
    pickerIndex: 0,
    highIndex: 0,
    manIndex: 3,
    type: ['车找人', '人找车'],
    isMan: false,
    isShow: null,
    picker: ['珠江新城到岑村', '岑村到珠江新城'],
    highSpeed: ['无','科韵路'],
    man: ['1人', '2人', '3人', '4人'],
    time: '12:01',
    date: '2019-11-20',
    startDate:'2019-11-20',
    phone:null,
    hfaddress:'',
    gyaddress:'',
    content:null
  },
  onLoad() {
    var nowDate = require('../../utils/util.js');
    this.setData({
      date: nowDate.getToday(),
      startDate: nowDate.getToday(),
      time: nowDate.getTime()
    })
  },
  TypeChange(e) {
    this.setData({
      typeIndex: e.detail.value
    })
    if (this.data.typeIndex == 0){
      this.setData({
        isMan: false,
        manIndex: 3
      })
    }else{
      this.setData({
        isMan: true,
        manIndex: 0
      })
    }
  },
  PickerChange(e) {
    this.setData({
      pickerIndex: e.detail.value
    })
  },
  HighChange(e) {
    this.setData({
      highIndex: e.detail.value
    })
  },
  ManChange(e) {
    this.setData({
      manIndex: e.detail.value
    })
  },
  HfaddChange(e) {
    this.setData({
      hfaddress: e.detail.value
    })
  },
  GyaddChange(e) {
    this.setData({
      gyaddress: e.detail.value
    })
  },
  PhoneChange(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  TimeChange(e) {
    this.setData({
      time: e.detail.value
    })
  },
  submit(e) {
    if (!(/^1[3456789]\d{9}$/.test(this.data.phone))){
      wx.showToast({
        title: '请输入11位手机号码！',
        icon: 'none',
        duration: 2000
      })
      return;
    } 
    if (this.data.hfaddress.length>5){
      wx.showToast({
        title: '地址输入不能超过5字符！',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.gyaddress.length > 5) {
      wx.showToast({
        title: '地址输入不能超过5字符！',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    var date = this.data.date.substring(5, this.data.date.length);
    var content = date + '日' + this.data.time + ',';
    //车找人
    if(this.data.typeIndex == 0){
      if (this.data.pickerIndex == 0){
        content += this.data.hfaddress + '到' + this.data.gyaddress
          + ',' + this.data.highSpeed[this.data.highIndex]+'上高速,可带'
          + this.data.man[this.data.manIndex] +',手机号：'+this.data.phone;
      }else{
        content +=  this.data.gyaddress + '到' + this.data.hfaddress
          + ',' + this.data.highSpeed[this.data.highIndex] + '下高速,可带'
          + this.data.man[this.data.manIndex] + ',手机号：' + this.data.phone;
      }
      //人找车
    }else{
      if (this.data.pickerIndex == 0) {
        content += '科学大道' + this.data.hfaddress + '到岑村' + this.data.gyaddress
          + ',' + this.data.man[this.data.manIndex] + '求带,手机号：' + this.data.phone;;
      } else {
        content += '岑村' + this.data.gyaddress + '到珠江新城' + this.data.hfaddress
          + ',' + this.data.man[this.data.manIndex] + '求带,手机号：' + this.data.phone;
      }
    }
    this.setData({
      content: content,
      isShow: 'show'
    })
  },
  hideModal() {
    this.setData({
      isShow: null
    })
  },
  addOrder(){
   this.saveDb();
  },
  saveDb() {
    this.skipHome();
    var nowDate = require('../../utils/util.js');
    var dbCon = this.data.content.substring(0, this.data.content.length - 16);
    // 1. 获取数据库引用
    const db = wx.cloud.database();
    db.collection('t_car_data').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        // _id: 'todo-identifiant-aleatoire', // 可选自定义 _id，在此处场景下用数据库自动分配的就可以了
        care_date: this.data.date,
        create_date: nowDate.getFormatTime(),
        create_time: new Date(),
        content: dbCon,
        phone: this.data.phone,
        type: this.data.typeIndex.toString(),
        way_type: this.data.pickerIndex.toString()
      }
    }).then(res => {
      try {
        this.hideModal();
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
      } catch (ex) {
        console.log(ex);
      }
    })
  },
  skipHome () {
    wx.navigateTo({
      url: '/pages/home/index'
    })
  },
})